/*
*  This addEvent function and associated functions were written by 
*  Dean Edwards, 2005 with input from Tino Zijdel, Matthias Miller, Diego Perini
*  http://dean.edwards.name/weblog/2005/10/add-event2/
*
*  This domLoad object is written by David Shuford (aka Kravvitz)
*  <http://www.dynamicsitesolutions.com/> is based on code written by 
*  Dean Edwards <http://dean.edwards.name/> (with input from John Resig 
*  <http://ejohn.org/> and Matthias Miller <http://www.outofhanwell.com/blog/>)
*  and two derivative works based on Dean's script by Rob Cherny 
*  <http://cherny.com/> and Tanny O'Haley <http://tanny.ica.com/>.
*
*  Free use of this script is permitted for commercial and non-commercial 
*  applications, subject to the requirement that this comment block be kept 
*  and not be altered.  The data and executable parts of the script may be 
*  changed as needed.  Dynamic Site Solutions makes no warranty regarding 
*  fitness of use or correct function of the script.  If you have any comments
*  or questions about this script, feel free to send them to 
*  <script_info@dynamicsitesolutions.com>.
*
*  Last Updated: 2008-03-22
*/

function addEvent(element,type,handler){
  if((type=="DOMContentLoaded") || (type.toLowerCase()=="domload")) {
    domLoad.add(handler);
    return;
  }
  if(element.addEventListener){
    element.addEventListener(type,handler,false);
  } else {
    // assign each event handler a unique ID
    if(!handler.$$guid) handler.$$guid=addEvent.guid++;
    // create a hash table of event types for the element
    if(!element.events) element.events={};
    // create a hash table of event handlers for each element/event pair
    var handlers=element.events[type];
    if(!handlers){
      handlers=element.events[type]={};
      // store the existing event handler (if there is one)
      if(element["on" + type]){
        handlers[0]=element["on" + type];
      }
    }
    // store the event handler in the hash table
    handlers[handler.$$guid]=handler;
    // assign a global event handler to do all the work
    element["on" + type]=handleEvent;
  }
};
// a counter used to create unique IDs
addEvent.guid=1;

function removeEvent(element,type,handler){
  if(element.removeEventListener){
    element.removeEventListener(type,handler,false);
  } else {
    // delete the event handler from the hash table
    if(element.events && element.events[type]){
      delete element.events[type][handler.$$guid];
    }
  }
};

function handleEvent(event){
  var returnValue=true;
  // grab the event object (IE uses a global event object)
  event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event,this);
  // get a reference to the hash table of event handlers
  var handlers=this.events[event.type];
  // execute each event handler
  for(var i in handlers){
    this.$$handleEvent=handlers[i];
    if(this.$$handleEvent(event) === false){
      returnValue=false;
    }
  }
  return returnValue;
};

function fixEvent(e,el){
  // add W3C standard event methods
  e.preventDefault=fixEvent.preventDefault;
  e.stopPropagation=fixEvent.stopPropagation;
  try{  // IE5/Mac generates an error here sometimes
    e.target=e.target||e.srcElement;
  }catch(ex){}
  e.currentTarget=e.currentTarget||el;
  if(e.type.indexOf('mouse')!=-1)
    e.relatedTarget=(e.relatedTarget?e.relatedTarget:
      (e.type=='mouseout'?e.toElement:e.fromElement));
  return e;
};
fixEvent.preventDefault=function(){
  this.returnValue=false;
};
fixEvent.stopPropagation=function(){
  this.cancelBubble=true;
};

// Tino Zijdel  http://therealcrisp.xs4all.nl/upload/addEvent_dean.html
// This little snippet fixes the problem that the onload attribute on the 
// body-element will overwrite previous attached events on the window object 
// for the onload event
if(!window.addEventListener){
  document.onreadystatechange=function(){
    if(window.onload && window.onload != handleEvent){
      addEvent(window,'load',window.onload);
      window.onload=handleEvent;
    }
  }
}


if(![].push){ // for old browsers that don't natively support Array.push()
  Array.prototype.push=function(){
    for(var i=0,l=arguments.length;i<l;i++) this[this.length]=arguments[i];
    return this.length;
  }
}

var domLoad={
  fnAr:[],
  inited:false,
  domDoneLoading:false,
  add:function(fn){
    (domLoad.domDoneLoading)?fn():this.fnAr.push(fn);
    this.init();
  },
  init:function(){
    if(this.inited) return;
    this.inited=true;
    // Mozilla (Firefox, NS7.x, etc.), Opera 9+
    if(document.addEventListener)
      document.addEventListener("DOMContentLoaded",domLoad.loaded,false);
    // for Safari, Konqueror, iCab
    if(/WebKit|KHTML|iCab/i.test(navigator.userAgent) && document.readyState){
      domLoad._timer=setInterval(function(){
        if(/loaded|complete/.test(document.readyState)){
          clearInterval(domLoad._timer);
          delete domLoad._timer;
          domLoad.loaded();
        }
      },10);
    }
    // for IE5+/Win (using their proprietary conditional compilation feature)
    /*@cc_on @if((@_win32 || @_win64) && (@_jscript_version >= 5))
      document.write("<script defer src='//:' onreadystatechange="+
        "\"if(this.readyState=='complete')domLoad.loaded();\"><\/script>");
    @end @*/
    // a normal window.onload handler will be used if none of the others work
    addEvent(window,'load',domLoad.loaded);
  },
  loaded:function(){
    if(domLoad.domDoneLoading) return;
    domLoad.domDoneLoading=true;
    // Safari 1.0 didn't like it when I used a local variable as a  
    // reference to domLoad.fnAr
    for(var i=0,l=domLoad.fnAr.length;i<l;i++)
      try{domLoad.fnAr[i]();}catch(ex){}
    domLoad.fnAr=[];
  }
};

